﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Processing001.Library;
using dotnetCHARTING.WinForms;
using Processing001.Library.AbstractClasses;
using Processing001.Library.Interfaces;

namespace Processing001
{
    public enum Graphic
    {
        Real,
        Sin,
        Cos,
        Left
    }

    public enum GraphicLab002
    {
        Y,
        Z,
        Wrapper,
        Kor
    }

    public enum GraphicLab003
    {
        
    }

    public enum Labs
    {
        Lab001 = 0,
        Lab002 = 1,
        Lab003 = 2,
        Lab004 = 3
    }

    public partial class MainForm : Form
    {
        private Dictionary<Graphic, Chart> graphics = new Dictionary<Graphic, Chart>();
        private Dictionary<GraphicLab002, Chart> graphics2 = new Dictionary<GraphicLab002, Chart>();
        private WorkerAbstract solution;
        private int DEFAULT_N = 8;

        private delegate void Add(Control control);

        public MainForm()
        {
            InitializeComponent();

            MainForm.CheckForIllegalCrossThreadCalls = false;
            GraphicsComboBox.SelectedIndex = 0;

            comboBox1.SelectedIndex = 0;
        }

        private void UpdateValues(object sender, EventArgs e)
        {
            foreach (ComplexNumber value in Solution001.Values)
            {
                String title = String.Format("({0}, {1})", value.Real, value.Image);
                listBox1.Items.Add(title);
            }
        }

        private void ResetConfiguration(object sender, EventArgs e)
        {
            container.Controls.Clear();
            generateButton.Enabled = true;
        }

        private void DisplayGraphics(object sender, EventArgs e)
        {
            DisplayLab001(0, graphics[Graphic.Real]);
            generateButton.Enabled = true;
        }

        private void DisplayLab001(int iterator, Chart chart)
        {
            GraphicsComboBox.SelectedIndex = iterator;
            container.Controls.Clear();
            container.Invoke(new Add(container.Controls.Add), graphics[(Graphic) iterator]);
        }

        private void DisplayLab002(int iterator, Chart chart)
        {
            GraphicsComboBox.SelectedIndex = iterator;
            container.Controls.Clear();
            container.Invoke(new Add(container.Controls.Add), graphics2[(GraphicLab002) iterator]);
        }

        private void generateButton_Click(object sender, EventArgs e)
        {
            generateButton.Enabled = false;
            UpdateModels(comboBox1.SelectedIndex);
            solution.Start();
        }

        private void DisplayGraphics_ForLab002(Object sender, EventArgs e)
        {
            if (graphics2.ContainsKey((GraphicLab002) GraphicsComboBox.SelectedIndex))
            {
                DisplayLab002(0, graphics2[GraphicLab002.Y]);
            }
            generateButton.Enabled = true;
        }

        private void GraphicsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisplayGraphicsContainer();
        }

        private void DisplayGraphicsContainer()
        {
            switch((Labs) comboBox1.SelectedIndex)
            {
                case Labs.Lab001:
                    if (graphics.ContainsKey((Graphic) GraphicsComboBox.SelectedIndex))
                    {
                        DisplayLab001(GraphicsComboBox.SelectedIndex, graphics[(Graphic)GraphicsComboBox.SelectedIndex]);
                    }
                    break;
                case Labs.Lab002:
                    if (graphics2.ContainsKey((GraphicLab002)GraphicsComboBox.SelectedIndex))
                    {
                        DisplayLab002(GraphicsComboBox.SelectedIndex, graphics2[(GraphicLab002)GraphicsComboBox.SelectedIndex]);
                    }
                    break;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateComboboxes();
            UpdateModels(comboBox1.SelectedIndex);
        }

        private void UpdateComboboxes()
        {
            GraphicsComboBox.Items.Clear();
            switch ((Labs) comboBox1.SelectedIndex)
            {
                case Labs.Lab001:
                    GraphicsComboBox.Items.AddRange(new String[] { "Реальный", "Частотный Мнимый", "Частотный Реальный", "Обратное" });
                    break;
                case Labs.Lab002:
                    GraphicsComboBox.Items.AddRange(new String[] { "Y", "Z", "Свёртка", "Корреляция" });
                    break;
            }
            GraphicsComboBox.SelectedIndex = 0;
        }

        private void UpdateModels(int index)
        {
            if (solution != null && solution.ResetConfiguration != null)
            {
                solution.ResetConfiguration -= new EventHandler(ResetConfiguration);
            }
            if (solution != null && solution.DisplayGraphics != null)
            {
                solution.DisplayGraphics -= new EventHandler(DisplayGraphics);
            }
            if (solution != null && solution.ValuesUpdate != null)
            {
                solution.ValuesUpdate -= new EventHandler(UpdateValues);
            }
            switch ((Labs) index)
            {
                case Labs.Lab001:
                    solution = new Solution001(DEFAULT_N);
                    ((Solution001)solution).Graphics = graphics;
                    solution.ResetConfiguration += new EventHandler(ResetConfiguration);
                    solution.DisplayGraphics += new EventHandler(DisplayGraphics);
                    solution.ValuesUpdate += new EventHandler(UpdateValues);
                    break;
                case Labs.Lab002:
                    solution = new Solution002(DEFAULT_N);
                    ((Solution002) solution).Graphics = graphics2;
                    solution.ResetConfiguration += new EventHandler(ResetConfiguration);
                    solution.DisplayGraphics += new EventHandler(DisplayGraphics_ForLab002);
//                    solution.ValuesUpdate += new EventHandler(UpdateValues);
                    break;
            }
        }
    }
}
